﻿using RobosimAPI.Class;
using System;
using System.Data.SQLite;
using System.Text.Json;
namespace RobosimAPI.Command
{
	public static partial class Command
	{
		public static void GetThemeList()
		{
			string httpResult = Tools.SignHttpPost("https://robosim.zmrobo.com/back/Api2/Client/themeList", "user_id=317047&region_id=0",Tools.GetTimestamp().ToString(), "86a0a3b1ea423db74cce0a50e533724c", "317047");
			ThemeList theme = JsonSerializer.Deserialize<ThemeList>(httpResult);
			using (var connection = new SQLiteConnection(Account.DB_CONNECTION_STRING))
			{
				connection.Open();
				using (var transaction = connection.BeginTransaction())
				{
					using (var command = new SQLiteCommand(connection))
					{
						for (int i = 0; i < theme.themeList.Count; i++)
						{
							command.CommandText = $"INSERT INTO themeList (cover, id,title,type,version) VALUES (@cover, @id, @title, @type, @version)";
							command.Parameters.AddWithValue("@cover", theme.themeList[i].cover);
							command.Parameters.AddWithValue("@id", theme.themeList[i].id);
							command.Parameters.AddWithValue("@title", theme.themeList[i].title);
							command.Parameters.AddWithValue("@type", theme.themeList[i].type);
							command.Parameters.AddWithValue("@version", theme.themeList[i].version);
							command.ExecuteNonQuery();
						}
					}
					transaction.Commit(); // 提交事务
				}
			}
		}
		public class ThemeListItem
		{
			public int id { get; set; }
			public string title { get; set; }
			public string cover { get; set; }
			public int type { get; set; }
			public string version { get; set; }
		}
		public class ThemeList
		{
			public int status { get; set; }
			public List<ThemeListItem> themeList { get; set; }
		}

	}
}
